﻿using Contract.Domain;
using MediatR;
using Trace.Infrastructure.Repository;
using Trace.Read_WebApi.Commands;

namespace Trace.Read_WebApi.CommandHandler
{
    public class GetTailCommandHandler : IRequestHandler<GetTailCommands, List<Tail>>
    {
        private readonly IRepository<Tail> repository;

        public GetTailCommandHandler(IRepository<Tail> repository)
        {
            this.repository = repository;
        }

        public async Task<List<Tail>> Handle(GetTailCommands request, CancellationToken cancellationToken)
        {
            var list = await repository.GetAllAsync();
            list = list.Where(x => x.ContractNumber == request.ContractNumber);
            list = list.OrderByDescending(x => x.DateTime);
            return list.ToList();
        }
    }
}
